package _021_10_7

import (
	"fmt"
	"strconv"
)

//使用了加法方式来做乘法，有趣，失败
func multiply2(num1 string, num2 string) string {
	result := make([]uint8, len(num1)+len(num2))
	for i := 0; i < len(result); i++ {
		result[i] = 0
	}
	bN := num2
	sN := num1
	if len(num1) > len(num2) {
		bN = num1
		sN = num2
	}
	carrNum := 0
	for i := 0; i < len(sN); i++ {
		n1, _ := strconv.Atoi(string(bN[i]))
		n2, _ := strconv.Atoi(string(sN[i]))
		temp := n1 * n2
		if carrNum > 0 {
			temp += carrNum
		}
		carrNum = temp / 10
		units := strconv.Itoa(temp % 10)
		result[len(result)-i-1] = units[0]
	}
	for i := len(sN); i < len(bN); i++ {
		if carrNum > 0 {
			n1, _ := strconv.Atoi(string(bN[i]))
			n1 += carrNum
			carrNum = n1 / 10
			n1 = n1 % 10
			num := strconv.Itoa(n1)
			result[len(result)-1-i] = num[0]
			continue
		}
		result[len(result)-1-i] = bN[i]
	}
	if carrNum != 0 {
		num := strconv.Itoa(carrNum)
		result[0] = num[0]
	}
	ret := ""
	for i := 0; i < len(result); i++ {
		if result[i] != 0 {
			ret += string(result[i])
		}
	}
	return ret
}

func multiply(num1 string, num2 string) string {
	if len(num1) == 0 || len(num2) == 0 || num1 == "0" || num2 == "0" {
		return "0"
	}
	result := make([]string, 0)
	bN := num2
	sN := num1
	if len(num1) > len(num2) {
		bN = num1
		sN = num2
	}

	for i := 0; i < len(sN); i++ {
		carrNum := uint8(0)
		n1 := sN[i] - '0'
		str := make([]string, 0)
		for j := 0; j < len(bN); j++ {
			n2 := bN[len(bN)-j-1] - '0'
			temp := n1 * n2
			if carrNum != 0 {
				temp += carrNum
			}
			carrNum = temp / 10
			temp %= 10
			str = append(str, string(temp+'0'))
		}
		if carrNum != 0 {
			str = append(str, string(carrNum+'0'))
		}
		for k := 0; k < len(str)/2; k++ {
			temp := str[k]
			str[k] = str[len(str)-k-1]
			str[len(str)-k-1] = temp
		}
		for k := 0; k < len(sN)-i-1; k++ {
			str = append(str, "0")
		}
		relStr := ""
		for k := 0; k < len(str); k++ {
			relStr += str[k]
		}
		result = append(result, relStr)
	}
	_result := make([]byte, 0)
	for i := 0; i < len(result[0]); i++ {
		_result = append(_result, result[0][i])
	}

	for i := 1; i < len(result); i++ {
		_carrNum := uint8(0)
		for k := 0; k < len(result[i]); k++ {
			n1 := _result[len(_result)-1-k] - '0'
			n2 := result[i][len(result[i])-1-k] - '0'
			_temp := n1 + n2
			if _carrNum != 0 {
				_temp += _carrNum
			}
			_carrNum = _temp / 10
			_temp %= 10
			_result[len(_result)-1-k] = _temp + '0'
		}
		if _carrNum != 0 {
			k := 0
			for k = len(result[i]); k < len(_result); k++ {
				n1 := _result[len(_result)-k-1] - '0'
				n1 += _carrNum
				_carrNum = n1 / 10
				n1 %= 10
				_result[len(_result)-k-1] = n1 + '0'
				if _carrNum == 0 {
					break
				}
			}
			if _carrNum != 0 {
				for z := 0; z < len(_result)/2; z++ {

					//_result[z] = _result[len(_result)-1-z] 源代码

					//下面是新代码
					temp := _result[z]                     //
					_result[z] = _result[len(_result)-1-z] //错误出现在这
					_result[len(_result)-1-z] = temp       //
				}
				_result = append(_result, '1')
				for z := 0; z < len(_result)/2; z++ {

					//_result[z] = _result[len(_result)-1-z] 源代码

					temp := _result[z]                     ////错误出现在这个地方
					_result[z] = _result[len(_result)-1-z] //
					_result[len(_result)-1-z] = temp       //
				}
			}
		}
		fmt.Println(string(_result))
	}
	relStr := ""
	for i := 0; i < len(_result); i++ {
		relStr += string(_result[i])
	}
	return relStr
}

//这段代码不在原数组上面加开辟一个新数组来操作，就会导致一直重复加，特麻烦

//_sN := result[i]
//_bN := result[i+1]
//if len(_sN) > len(_bN) {
//_sN = result[i+1]
//_bN = result[i]
//}
//_carrNum := uint8(0)
//_ret := make([]byte, 0)
//for k := 0; k < len(_sN); k++ {
//n1 := _sN[len(sN)-1-k] - '0'
//n2 := _bN[len(bN)-1-k] - '0'
//temp := n1 + n2
//if _carrNum != 0 {
//temp += _carrNum
//}
//_carrNum = temp / 10
//temp %= 10
//_ret = append(_ret, temp+'0')
//}
//if _carrNum != 0 {
//_ret = append(_ret, _carrNum+'0')
//}
//for k := 0; k < len(_ret)/2; k++ {
//_ret[k] = _ret[len(_ret)-k-1]
//}
